$(document).ready(function(){
	initThickboxLinks();
	initQuizItems();
	initDrop();
	initSurveyLink();
	initSurvey();
	apropos();
});

jQuery(document).ready(function($) {
  $('a[rel*=facebox]').facebox()
}) 

apropos = function(){
	$("#a-propos-but").each( function(){
			$(this).attr({'rel': "facebox"});
			$(this).attr({'href': "#a-propos"});
			$(this).attr({'name': "Pegasus"});
			$(this).attr({'target': "_window"});
	});
}

initThickboxLinks = function(){
	
	$("div.popin>a , a.multiple-question" ).each( 
		function(){
			$(this).attr({'rel': "facebox"});
			$(this).attr({'href': "#"+$(this).attr('name')});
			$(this).attr({'name': "Pegasus"});
			$(this).attr({'target': "_window"});
		}
	)
	
	$("div.hover>a").tooltip({ 
    	bodyHandler: function() { 
        	return $("#"+$(this).attr("name")).html(); 
    	}, 
	    showURL: false 
	});

}

countChecked = function(){
	return $(document).find("input:checked[@value='ans1']").size();
}

initSurveyLink =function(){
	$(document).find("#show-result").each(function(){	
		$(this).attr({'rel': "facebox"});
		$(this).attr({'href': "#res-0-4"});
		$(this).attr({'name': "Pegasus"});
		$(this).attr({'target': "_window"});
	});
}

initSurvey = function(){
	var nb;
	$('input').click(function(){
		var nb = countChecked();
		
		var id;
		if(nb<4) id="res-0-4";
		else if(nb<6) id="res-4-6";
		else id="res-6-10";

		$(document).find("#show-result").each(function(){
			$(this).attr({'rel': "facebox"});
			$(this).attr({'href': "#"+id});
			$(this).attr({'name': "Pegasus"});
			$(this).attr({'target': "_window"});
		});
		
	});
	
}

initDrop = function(){
    $(".block").draggable({revert:true}); //helper: 'clone', 
    var droppable = $(".drop");
    
	$(".drop").each( function(){
		$(this).droppable({
			accept: "."+$(this).attr('id'),
			tolerance: "touch",
			activeClass: 'droppable-active',
			hoverClass: 'droppable-hover',
			drop: function(ev,ui) {
					$(this).append(ui.draggable); 
				}
		})
	});
}


initQuizItems = function(){
	var arrayAns = {}; 
	
	$("table.quiz-answers").find('tbody tr td').
	each(function() { 
  		arrayAns[$(this).text().toLowercase] = $(this).text().toLowercase; 
	});
	
	
	$("div.quiz-item").each( function(){
		var answer = $("div.answer",this).text();
		var comment = $("div.comment",this).text();
		
		$("input",this).keyup(
			function(){
				if(this.value==''){
					$(this).next('span').text("");
				}
						
				else if(this.value.toLowerCase()==answer.toLowerCase()){
					$(this).next('span').text("Correcte. "+comment);
				}
				
				else if(arrayAns[this.value.toLowerCase]==this.value.toLowerCase && 
						this.value.toLowerCase()!= answer.toLowerCase()){		
					
					$(this).next('span').text("Incorrecte.");
					
				}	
			}
		);
		
		$("input",this).blur(
			function(){
				if(this.value==''){
					$(this).next('span').text("");
				}
				
				else if(this.value.toLowerCase()!= answer.toLowerCase()){
					$(this).next('span').html('<a class="giveAns" href="">&nbsp;Incorrecte. Voir la r&#233;ponse?</a>');
					
					$('a.giveAns').click(function(){
						$(this.parentNode.parentNode.parentNode).find('div.comment').css({display : 'block'});
						return false;
					});
				}
				
				else if(this.value.toLowerCase()==answer.toLowerCase()){
					$(this).next('span').text("Correcte. "+comment);
				}
			}
		);
	}
	);
}
